package org.kazuhiko.rpg.client.net;

import org.kazuhiko.rpg.client.net.packet.PacketBuilder;
import org.kazuhiko.rpg.objects.Player;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ActionSender
{
	private static final Logger logger = LoggerFactory.getLogger( ActionSender.class );
	
	private RPGSession session;
	
	/**
	 * ---------------------
	 * Outgoing packets
	 * 0 = send authentication request
	 * 1 = send position
	 * ---------------------
	 */
	
	public ActionSender( RPGSession session )
	{
		this.session = session;
	}
	
	public void sendAuthenticationRequest( String username, String password )
	{
		PacketBuilder bldr = new PacketBuilder( session.getPackets().getOutgoingPacket( 0 ) ); //Remember the 0!
		bldr.putClassicString( "username", username );
		bldr.putClassicString( "key", password );
		
		logger.trace( "Sending authentication request for user {} ", username );
		
		session.send( bldr.toPacket() );
	}
	
	public void sendPosition( Player player )
	{
		PacketBuilder bldr = new PacketBuilder( session.getPackets().getOutgoingPacket( 1 ) );
		bldr.putByte( "id", player.getId() );
		bldr.putClassicString( "name", player.getName() );
		bldr.putShort( "x", (int)player.getPosition().x );
		bldr.putShort( "y", (int)player.getPosition().y );
		bldr.putShort( "z", (int)player.getPosition().z );
		bldr.putShort( "pitch", (int)player.getRotation().x );
		bldr.putShort( "yaw", (int)player.getRotation().y );
		bldr.putShort( "roll", (int)player.getRotation().z );
		
		logger.trace( "Sending position" );
		
		session.send( bldr.toPacket() );
	}
}
